package com.zc.analysis.serialport;


import com.ruoyi.common.core.redis.RedisCache;
import com.zc.common.constant.RedisKeyConstants;
import com.zc.core.message.CommonDeviceMessage;
import com.zc.core.message.DeviceMessage;
import com.zc.core.message.DeviceOfflineMessage;
import com.zc.core.message.DeviceOnlineMessage;
import com.zc.core.message.property.ReportPropertyMessage;
import com.zc.operator.domain.ChargeStationDeviceExtend;
import com.zc.operator.mapper.ChargeStationDeviceExtendMapper;
import com.zc.util.ArithmeticUtils;
import com.zc.util.RadixUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import reactor.core.publisher.Flux;


import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.*;


import static com.zc.util.RadixUtil.AddBinToDec;
import static com.zc.util.RadixUtil.hexToDecimal;
//import static sun.plugin.security.JDK11ClassFileTransformer.init;

public class SerialPortCallback {

    private  final Logger log =  LoggerFactory.getLogger(this.getClass());

    private static Logger logs = LoggerFactory.getLogger(SerialPortCallback.class);

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ChargeStationDeviceExtendMapper chargeStationDeviceExtendMapper;


    private static Properties properties = new Properties();

    private static JSONObject m_data = new JSONObject();

    private static Map<String,Object> map = new HashMap<>();

    /*static {
        InputStream is = SerialPortCallback.class.getClassLoader().getResourceAsStream("config.properties");
        try {
            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
            properties.load(isr);
        } catch (IOException e) {
            logs.error("加载电表解析配置文件出错",e);
        }
        //初始化数据
        init();
    }*/

    public void dataAvailable() {
        try {
            byte[] data = SerialPortManager.readSerialPortData();
            String s = ConvertHexStrAndStrUtils.bytesToHexStr(data);
            String s1 = addSpace(s);
            log.info("rev--data:"+s);
            log.info("rev--data:"+s1);
            if("68".equals(s.substring(0,2))){
                analysis(s1);
            }else{
                modbusAnalysis(s);
            }
        } catch (Exception e) {
            log.error(e.toString());
        }
    }

    //DL/T645协议解析
    public static void analysis(String command) throws Exception {
        //解析报文格式
        String[] newCommands = command.trim().split(" ");
        //newCommands.length > 26
        if (newCommands.length < 16 || newCommands.length > 35 || Integer.parseInt(newCommands[0]) != 68 || Integer.parseInt(newCommands[newCommands.length - 1]) != 16) {
            System.err.print("非法帧，无法解析！");

        } else {
            logs.info("报文源码：" + command);
            logs.info("帧起始符：" + newCommands[0]);
            String meter_serial = newCommands[6].concat(newCommands[5]).concat(newCommands[4]).concat(newCommands[3]).concat(newCommands[2]).concat(newCommands[1]);
            logs.info("电表地址：" + meter_serial);
            logs.info("控制域：" + newCommands[8]);
            logs.info("数据域长度：" + newCommands[9]);
            logs.info("校验码：" + newCommands[newCommands.length - 2]);
            logs.info("停止位：" + newCommands[newCommands.length - 1]);

            //取单个电表地址，组成单个对象
            String a1=Integer.toHexString(Integer.parseInt(newCommands[13],16) - 51);
            String a2=Integer.toHexString(Integer.parseInt(newCommands[12],16) - 51);
            String a3=Integer.toHexString(Integer.parseInt(newCommands[11],16) - 51);
            String a4=Integer.toHexString(Integer.parseInt(newCommands[10],16) - 51);
            String DTID0 = newCommands[13].equals("32")?newCommands[13] = "FF" :(a1.length() > 1 ? a1:"0".concat(a1));
            String DTID1 = newCommands[12].equals("32")?newCommands[12] = "FF" :(a2.length() > 1 ? a2:"0".concat(a2));
            String DTID2 = newCommands[11].equals("32")?newCommands[11] = "FF" :(a3.length() > 1 ? a3:"0".concat(a3));
            String DTID3 = newCommands[10].equals("32")?newCommands[10] = "FF" :(a4.length() > 1 ? a4:"0".concat(a4));

            String sbr = DTID0.concat(DTID1).concat(DTID2).concat(DTID3);
            logs.info(sbr);
            logs.info("数据项名称：" + properties.getProperty(sbr));

            //解析返回数据
            if (newCommands.length > 16) {
                List<String> list3 = new ArrayList<String>();
                for (int i = 0; i < Integer.parseInt(newCommands[9], 16) - 4; i++) {
                    list3.add(newCommands[newCommands.length - 3 - i]);
                }
                String[] data = list3.toArray(new String[list3.size()]);
                String num = (dataFormat(data)).toString();
                System.out.println("num:"+num);
                BigDecimal bigDecimal = new BigDecimal(num);

                if (DTID0.equals("02") && DTID1.equals("01") && !DTID2.equals("FF")) { //电压0.1v
                    BigDecimal UA = bigDecimal.multiply(new BigDecimal("0.1"));
                    logs.info(properties.getProperty(sbr) + "：" + UA + "v");
                    m_data.put("UA",UA.doubleValue());
                } else if (DTID0.equals("02") && DTID1.equals("02") && !DTID2.equals("FF")) { //电流0.001A
                    String isNegative = bigDecimal.toString().substring(0,1).equals("8") ? "0".concat(bigDecimal.toString().substring(1)):bigDecimal.toString();
                    BigDecimal IA = new BigDecimal(isNegative).multiply(new BigDecimal("0.001"));
                    logs.info(properties.getProperty(sbr) + "：" + IA + "A");
                    m_data.put("IA",IA.doubleValue());

                } else if ((DTID0.equals("02") && DTID1.equals("03")&& !DTID2.equals("FF")) || (DTID0.equals("02") && DTID1.equals("04")) && !DTID2.equals("FF")|| (DTID0.equals("02") && DTID1.equals("05")&& !DTID2.equals("FF"))) { //有无功功率0.0001kW
                    String isNegative = bigDecimal.toString().substring(0,1).equals("8") ? "0".concat(bigDecimal.toString().substring(1)):bigDecimal.toString();
                    BigDecimal POWER_TOTAL = new BigDecimal(isNegative).multiply(new BigDecimal("0.0001"));
                    logs.info(properties.getProperty(sbr) + "：" + POWER_TOTAL);
                    m_data.put("POWER",POWER_TOTAL.doubleValue());

                } else if (DTID0.equals("02") && DTID1.equals("06")&& !DTID2.equals("FF")) { //功率因数0.001
                    String isNegative = bigDecimal.toString().substring(0,1).equals("8") ? "0".concat(bigDecimal.toString().substring(1)):bigDecimal.toString();
                    String pf =Integer.parseInt(isNegative) > 1000 ? "1000" : isNegative;
                    BigDecimal POWER_FACTOR = new BigDecimal(pf).multiply(new BigDecimal("0.001"));
                    logs.info(properties.getProperty(sbr) + "：" + POWER_FACTOR);
                    m_data.put("POWER_FACTOR",POWER_FACTOR.doubleValue());

                } else if (DTID0.equals("00") && DTID1.equals("01")) { //正向有功总电能0.01
                    BigDecimal EpAdd = bigDecimal.multiply(new BigDecimal("0.01"));
                    logs.info(properties.getProperty(sbr) + "：" + EpAdd);
                    m_data.put("Ep+",EpAdd.doubleValue());
                } else if (DTID0.equals("00") && DTID1.equals("02")) { //反向有功总电能0.01
                    BigDecimal USE_TOTAL = bigDecimal.multiply(new BigDecimal("0.01"));
                    logs.info(properties.getProperty(sbr) + "：" + USE_TOTAL);
                    m_data.put("Ep-",USE_TOTAL.doubleValue());
                } else if (DTID0.equals("02") && DTID3.equals("02")) { //电网频率0.01
                    BigDecimal Fr = bigDecimal.multiply(new BigDecimal("0.01"));
                    logs.info(properties.getProperty(sbr) + "：" + Fr);
                    m_data.put("Fr",Fr.doubleValue());
                } else if (DTID0.equals("02") && DTID1.equals("01") && DTID2.equals("FF")) { //电压数据块
                    BigDecimal UC = new BigDecimal(String.valueOf(num).substring(0, 4)).multiply(new BigDecimal("0.1"));
                    logs.info("C相电压" + UC);
                    BigDecimal UB = new BigDecimal(String.valueOf(num).substring(4, 8)).multiply(new BigDecimal("0.1"));
                    logs.info("B相电压" + UB);
                    BigDecimal UA = new BigDecimal(String.valueOf(num).substring(8)).multiply(new BigDecimal("0.1"));
                    logs.info("A相电压" + UA);
                    m_data.put("UA",UA.doubleValue());
                    m_data.put("UB",UB.doubleValue());
                    m_data.put("UC",UC.doubleValue());

                } else if (DTID0.equals("02") && DTID1.equals("02") && DTID2.equals("FF")) { //电流数据块
                    String icString = String.valueOf(num).substring(0, 6);
                    String icisNegative = icString.substring(0,1).equals("8") ? "0".concat(icString.substring(1)):icString;
                    BigDecimal IC = new BigDecimal(icisNegative).multiply(new BigDecimal("0.001"));
                    logs.info("C相电流" + IC);
                    String ibString = String.valueOf(num).substring(6, 12);
                    String ibisNegative = ibString.substring(0,1).equals("8") ? "0".concat(ibString.substring(1)):ibString;
                    BigDecimal IB = new BigDecimal(ibisNegative).multiply(new BigDecimal("0.001"));
                    logs.info("B相电流" + IB);
                    String iaString = String.valueOf(num).substring(12);
                    String iaisNegative = iaString.substring(0,1).equals("8") ? "0".concat(iaString.substring(1)):iaString;
                    BigDecimal IA = new BigDecimal(iaisNegative).multiply(new BigDecimal("0.001"));
                    logs.info("A相电流" + IA);
                    m_data.put("IA",IA.doubleValue());
                    m_data.put("IB",IB.doubleValue());
                    m_data.put("IC",IC.doubleValue());
                }else if (DTID0.equals("02") && DTID1.equals("03") && DTID2.equals("FF")) { //有功功率数据块
                    String PcString = String.valueOf(num).substring(0, 6);
                    String PcIsNegative = PcString.substring(0,1).equals("8") ? "0".concat(PcString.substring(1)):PcString;
                    BigDecimal PC = new BigDecimal(PcIsNegative).multiply(new BigDecimal("0.0001"));
                    logs.info("C相有功功率" + PC);
                    String PbString = String.valueOf(num).substring(6, 12);
                    String PbIsNegative = PbString.substring(0,1).equals("8") ? "0".concat(PbString.substring(1)):PbString;
                    BigDecimal PB = new BigDecimal(PbIsNegative).multiply(new BigDecimal("0.0001"));
                    logs.info("B相有功功率" + PB);
                    String PaString = String.valueOf(num).substring(12,18);
                    String PaIsNegative = PaString.substring(0,1).equals("8") ? "0".concat(PaString.substring(1)):PaString;
                    BigDecimal PA = new BigDecimal(PaIsNegative).multiply(new BigDecimal("0.0001"));
                    logs.info("A相有功功率" + PA);
                    String PString = String.valueOf(num).substring(12,18);
                    String PIsNegative = PString.substring(0,1).equals("8") ? "0".concat(PString.substring(1)):PString;
                    BigDecimal P = new BigDecimal(PIsNegative).multiply(new BigDecimal("0.0001"));
                    logs.info("总有功功率" + P);
                    m_data.put("PA",PA.doubleValue());
                    m_data.put("PB",PB.doubleValue());
                    m_data.put("PC",PC.doubleValue());
                    m_data.put("P",P.doubleValue());
                }else if (DTID0.equals("02") && DTID1.equals("04") && DTID2.equals("FF")) { //有功功率数据块
                    String QcString = String.valueOf(num).substring(0, 6);
                    String QcIsNegative = QcString.substring(0,1).equals("8") ? "0".concat(QcString.substring(1)):QcString;
                    BigDecimal QC = new BigDecimal(QcIsNegative).multiply(new BigDecimal("0.0001"));
                    logs.info("C相无功功率" + QC);
                    String QbString = String.valueOf(num).substring(6, 12);
                    String QbIsNegative = QbString.substring(0,1).equals("8") ? "0".concat(QbString.substring(1)):QbString;
                    BigDecimal QB = new BigDecimal(QbIsNegative).multiply(new BigDecimal("0.0001"));
                    logs.info("B相无功功率" + QB);
                    String QaString = String.valueOf(num).substring(12,18);
                    String QaIsNegative = QaString.substring(0,1).equals("8") ? "0".concat(QaString.substring(1)):QaString;
                    BigDecimal QA = new BigDecimal(QaIsNegative).multiply(new BigDecimal("0.0001"));
                    logs.info("A相无功功率" + QA);
                    String QString = String.valueOf(num).substring(12,18);
                    String QIsNegative = QString.substring(0,1).equals("8") ? "0".concat(QString.substring(1)):QString;
                    BigDecimal Q = new BigDecimal(QIsNegative).multiply(new BigDecimal("0.0001"));
                    logs.info("总无功功率" + Q);
                    m_data.put("QA",QA.doubleValue());
                    m_data.put("QB",QB.doubleValue());
                    m_data.put("QC",QC.doubleValue());
                    m_data.put("Q",Q.doubleValue());

                }else if (DTID0.equals("02") && DTID1.equals("06") && DTID2.equals("FF")) { //有功功率数据块

                    String PfcString = String.valueOf(num).substring(0, 4);
                    String PfcIsNegative = PfcString.substring(0,1).equals("8") ? "0".concat(PfcString.substring(1)):PfcString;
                    BigDecimal Pfc = new BigDecimal(Integer.parseInt(PfcIsNegative) > 1000 ? "1000" : PfcIsNegative).multiply(new BigDecimal("0.001"));
                    logs.info("C相功率因数" + Pfc);

                    String PfbString = String.valueOf(num).substring(4, 8);
                    String PfbIsNegative = PfbString.substring(0,1).equals("8") ? "0".concat(PfbString.substring(1)):PfbString;
                    BigDecimal Pfb = new BigDecimal(Integer.parseInt(PfbIsNegative) > 1000 ? "1000" : PfbIsNegative).multiply(new BigDecimal("0.0001"));
                    logs.info("B相功率因数" + Pfb);

                    String PfaString = String.valueOf(num).substring(8,12);
                    String PfaIsNegative = PfaString.substring(0,1).equals("8") ? "0".concat(PfaString.substring(1)):PfaString;
                    BigDecimal Pfa = new BigDecimal(Integer.parseInt(PfaIsNegative) > 1000 ? "1000" : PfaIsNegative).multiply(new BigDecimal("0.0001"));
                    logs.info("A相功率因数" + Pfa);

                    String PfString = String.valueOf(num).substring(12,16);
                    String PfIsNegative = PfString.substring(0,1).equals("8") ? "0".concat(PfString.substring(1)):PfString;
                    BigDecimal Pf = new BigDecimal(Integer.parseInt(PfIsNegative) > 1000 ? "1000" : PfIsNegative).multiply(new BigDecimal("0.0001"));
                    logs.info("总相功率因数" + Pf);
                    m_data.put("Pfc",Pfc.doubleValue());
                    m_data.put("Pfb",Pfb.doubleValue());
                    m_data.put("Pfa",Pfa.doubleValue());
                    m_data.put("Pf",Pf.doubleValue());
                }else if (DTID0.equals("02") && DTID1.equals("05") && DTID2.equals("FF")) { //有功功率数据块
                    String ScString = String.valueOf(num).substring(0, 6);
                    String ScIsNegative = ScString.substring(0,1).equals("8") ? "0".concat(ScString.substring(1)):ScString;
                    BigDecimal SC = new BigDecimal(ScIsNegative).multiply(new BigDecimal("0.0001"));
                    logs.info("C相视在功率" + SC);
                    String SbString = String.valueOf(num).substring(6, 12);
                    String SbIsNegative = SbString.substring(0,1).equals("8") ? "0".concat(SbString.substring(1)):SbString;
                    BigDecimal SB = new BigDecimal(SbIsNegative).multiply(new BigDecimal("0.0001"));
                    logs.info("B相视在功率" + SB);
                    String SaString = String.valueOf(num).substring(12,18);
                    String SaIsNegative = SaString.substring(0,1).equals("8") ? "0".concat(SaString.substring(1)):SaString;
                    BigDecimal SA = new BigDecimal(SaIsNegative).multiply(new BigDecimal("0.0001"));
                    logs.info("A相视在功率" + SA);
                    String SString = String.valueOf(num).substring(12,18);
                    String SIsNegative = SString.substring(0,1).equals("8") ? "0".concat(SString.substring(1)):SString;
                    BigDecimal S = new BigDecimal(SIsNegative).multiply(new BigDecimal("0.0001"));
                    logs.info("总视在功率" + S);
                    m_data.put("SA",SA.doubleValue());
                    m_data.put("SB",SB.doubleValue());
                    m_data.put("SC",SC.doubleValue());
                    m_data.put("S",S.doubleValue());
                } else {
                    logs.error(properties.getProperty(sbr) + "：" + num+"解析时未找到合适的数据标识！");
                }

            }

        }
        System.out.println("m_data:"+m_data);
    }

    //modbus rtu协议解析
    public  void modbusAnalysis(String command){
        String len= command.substring(4,6);
        if("34".equals(len)){
            double Ua = ArithmeticUtils.mul(Double.parseDouble(hexToDecimal(command.substring(6, 10))), 0.1, 2);
            double Ub = ArithmeticUtils.mul(Double.parseDouble(hexToDecimal(command.substring(10, 14))), 0.1, 2);
            double Uc = ArithmeticUtils.mul(Double.parseDouble(hexToDecimal(command.substring(14, 18))), 0.1, 2);

            double Ia = ArithmeticUtils.mul(Double.parseDouble(hexToDecimal(command.substring(18, 22))), 0.01, 2);
            double Ib = ArithmeticUtils.mul(Double.parseDouble(hexToDecimal(command.substring(22, 26))), 0.01, 2);
            double Ic = ArithmeticUtils.mul(Double.parseDouble(hexToDecimal(command.substring(26, 30))), 0.01, 2);

            double Pa = ArithmeticUtils.mul(Double.parseDouble(AddBinToDec(RadixUtil.hex2Binary(command.substring(30, 34)), true)), 0.001, 3);
            double Pb = ArithmeticUtils.mul(Double.parseDouble(AddBinToDec(RadixUtil.hex2Binary(command.substring(34, 38)), true)), 0.001, 3);
            double Pc = ArithmeticUtils.mul(Double.parseDouble(AddBinToDec(RadixUtil.hex2Binary(command.substring(38, 42)), true)), 0.001, 3);
            double P = ArithmeticUtils.mul(Double.parseDouble(AddBinToDec(RadixUtil.hex2Binary(command.substring(42, 46)), true)), 0.001, 3);

            double Qa = ArithmeticUtils.mul(Double.parseDouble(AddBinToDec(RadixUtil.hex2Binary(command.substring(46, 50)), true)), 0.001, 3);
            double Qb = ArithmeticUtils.mul(Double.parseDouble(AddBinToDec(RadixUtil.hex2Binary(command.substring(50, 54)), true)), 0.001, 3);
            double Qc = ArithmeticUtils.mul(Double.parseDouble(AddBinToDec(RadixUtil.hex2Binary(command.substring(54, 58)), true)), 0.001, 3);
            double Q = ArithmeticUtils.mul(Double.parseDouble(AddBinToDec(RadixUtil.hex2Binary(command.substring(58, 62)), true)), 0.001, 3);

            double Sa = ArithmeticUtils.mul(Double.parseDouble(AddBinToDec(RadixUtil.hex2Binary(command.substring(62, 66)), true)), 0.001, 3);
            double Sb = ArithmeticUtils.mul(Double.parseDouble(AddBinToDec(RadixUtil.hex2Binary(command.substring(66, 70)), true)), 0.001, 3);
            double Sc = ArithmeticUtils.mul(Double.parseDouble(AddBinToDec(RadixUtil.hex2Binary(command.substring(70, 74)), true)), 0.001, 3);
            double S = ArithmeticUtils.mul(Double.parseDouble(AddBinToDec(RadixUtil.hex2Binary(command.substring(74, 78)), true)), 0.001, 3);

            double Pfa = ArithmeticUtils.mul(Double.parseDouble(AddBinToDec(RadixUtil.hex2Binary(command.substring(78, 82)), true)), 0.001, 3);
            double Pfb = ArithmeticUtils.mul(Double.parseDouble(AddBinToDec(RadixUtil.hex2Binary(command.substring(82, 86)), true)), 0.001, 3);
            double Pfc = ArithmeticUtils.mul(Double.parseDouble(AddBinToDec(RadixUtil.hex2Binary(command.substring(86, 90)), true)), 0.001, 3);
            double Pf = ArithmeticUtils.mul(Double.parseDouble(AddBinToDec(RadixUtil.hex2Binary(command.substring(90, 94)), true)), 0.001, 3);

            double Fr = ArithmeticUtils.mul(Double.parseDouble(hexToDecimal(command.substring(94, 98))), 0.01, 2);

            double Uab = ArithmeticUtils.mul(Double.parseDouble(hexToDecimal(command.substring(98, 102))), 0.1, 2);
            double Ubc = ArithmeticUtils.mul(Double.parseDouble(hexToDecimal(command.substring(102, 106))), 0.1, 2);
            double Uca = ArithmeticUtils.mul(Double.parseDouble(hexToDecimal(command.substring(106, 110))), 0.1, 2);
            map.put("Ua",Ua);
            map.put("Ub",Ub);
            map.put("Uc",Uc);
            map.put("Ia",Ia);
            map.put("Ib",Ib);
            map.put("Ic",Ic);
            map.put("Pa",Pa);
            map.put("Pb",Pb);
            map.put("Pc",Pc);
            map.put("P",P);
            map.put("Qa",Qa);
            map.put("Qb",Qb);
            map.put("Qc",Qc);
            map.put("Q",Q);
            /*map.put("Sa",Sa);
            map.put("Sb",Sb);
            map.put("Sc",Sc);
            map.put("S",S);
            map.put("Pfa",Pfa);
            map.put("Pfb",Pfb);
            map.put("Pfc",Pfc);
            map.put("Pf",Pf);*/
            map.put("Sc",Sc);
            map.put("Fr",Fr);
            map.put("Uab",Uab);
            map.put("Ubc",Ubc);
            map.put("Uca",Uca);
            System.out.println(map);
        }else if("28".equals(len)){

            double PPower = ArithmeticUtils.mul(Double.parseDouble(hexToDecimal(command.substring(6, 14))), 0.01, 2);
            double PPowerJ = ArithmeticUtils.mul(Double.parseDouble(hexToDecimal(command.substring(14, 22))), 0.01, 2);
            double PPowerF = ArithmeticUtils.mul(Double.parseDouble(hexToDecimal(command.substring(22, 30))), 0.01, 2);
            double PPowerP = ArithmeticUtils.mul(Double.parseDouble(hexToDecimal(command.substring(30, 38))), 0.01, 2);
            double PPowerG = ArithmeticUtils.mul(Double.parseDouble(hexToDecimal(command.substring(38, 46))), 0.01, 2);

            double QPower = ArithmeticUtils.mul(Double.parseDouble(hexToDecimal(command.substring(46, 54))), 0.01, 2);
            double QPowerJ = ArithmeticUtils.mul(Double.parseDouble(hexToDecimal(command.substring(54, 62))), 0.01, 2);
            double QPowerF = ArithmeticUtils.mul(Double.parseDouble(hexToDecimal(command.substring(62, 70))), 0.01, 2);
            double QPowerP = ArithmeticUtils.mul(Double.parseDouble(hexToDecimal(command.substring(70, 78))), 0.01, 2);
            double QPowerG = ArithmeticUtils.mul(Double.parseDouble(hexToDecimal(command.substring(78, 86))), 0.01, 2);
            map.put("EPI",PPower);
            /*map.put("PPowerJ",PPowerJ);
            map.put("PPowerF",PPowerF);
            map.put("PPowerP",PPowerP);
            map.put("PPowerG",PPowerG);*/
            map.put("EPE",QPower);
            /*map.put("QPowerJ",QPowerJ);
            map.put("QPowerF",QPowerF);
            map.put("QPowerP",QPowerP);
            map.put("QPowerG",QPowerG);*/
            dataParsing(map,command.substring(0,2));
        }
    }


    public void dataParsing(Map<String,Object> map,String address){
        Double p = Double.valueOf(String.valueOf(map.get("P")));
        ChargeStationDeviceExtend chargeStationDeviceExtend=new ChargeStationDeviceExtend();
        chargeStationDeviceExtend.setAddressTwo(address);
        List<DeviceMessage> messageList = new ArrayList<>();
        List<ChargeStationDeviceExtend> chargeStationDeviceExtends = chargeStationDeviceExtendMapper.selectChargeStationDeviceExtendList(chargeStationDeviceExtend);
        if(chargeStationDeviceExtends.size()>0){
            ChargeStationDeviceExtend chargeStationDeviceExtendData = chargeStationDeviceExtends.get(0);
            String stationCode = chargeStationDeviceExtendData.getStationCode();
            String deviceId = chargeStationDeviceExtendData.getDeviceId();
            redisCache.setCacheMapValue(RedisKeyConstants.STATION_DEVICE_REAL_POWER.replaceAll(":station:device", ":1001:"), deviceId, p);
            ReportPropertyMessage messageUpload = new ReportPropertyMessage();
            messageUpload.setDeviceId(deviceId);
            messageUpload.setTimestamp(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
            // 构造上报属性
            messageUpload.setProperties(map);
            messageList.add(messageUpload);

            String comMunStatus="1";
            //设置设备状态
            CommonDeviceMessage messageCollector = "1".equals(comMunStatus) ? new DeviceOnlineMessage() : new DeviceOfflineMessage();
            messageCollector.setDeviceId(deviceId);
            messageList.add(messageCollector);
            //PlatformDeviceDataAccessSupport.handlerClientDeviceMessage(Flux.fromIterable(messageList));
        }
    }












    public static StringBuffer dataFormat(String data[]){
        StringBuffer sbr=new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            String data1=String.valueOf(Integer.parseInt(data[i].substring(0,1),16)-3);
            String data2=String.valueOf(Integer.parseInt(data[i].substring(1),16)-3);
            sbr.append(data1);
            sbr.append(data2);
        }
        return sbr;
    }

    public static String addSpace(String str) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            // 添加当前字符
            result.append(str.charAt(i));
            // 每隔两个字符添加一个空格
            if ((i + 1) % 2 == 0) {
                result.append(" ");
            }
        }
        return result.toString();
    }




}
